package com.lyh.servlet;

import com.lyh.pojo.Book;
import com.lyh.pojo.BookVo;
import com.lyh.pojo.PageBean;
import com.lyh.service.BookService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 功能
 * 作者：lyh
 * 日期：2024/8/8 10:50
 */
@WebServlet("/show")
public class bookShow extends HttpServlet {
    BookService bookService = new BookService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收参数
        String likeName = request.getParameter("likeName");


//        List<BookVo> bookList= bookService.findAll();
//        //bookList 存到request域
//        request.setAttribute("bookList",bookList);

        String pageNum1 = request.getParameter("pageNum");
        ////当前页         空串把1赋值给当前页 否则做类型转换成int
        int pageNum = (pageNum1==null||pageNum1.trim()=="")?1:Integer.parseInt(pageNum1);
        //每页记录数
        int pageSize=3;
        //调用业务方法 返回分页对象
//        PageBean<BookVo> pageBean = bookService.findAll(pageNum,pageSize);

        PageBean<BookVo> pageBean =null;
        if (likeName==null||likeName.trim()==""){
            pageBean = bookService.findAll(pageNum,pageSize);
        }else {
            pageBean = bookService.like(pageNum,pageSize,likeName);
            //如果查询条件不为空 把likeName放到request域中
            request.setAttribute("likeName",likeName);
        }
        request.setAttribute("pageBean",pageBean);
        request.getRequestDispatcher("/jsp/bookShow.jsp").forward(request,response);

    }
}
